Imports OutlookStyleControls
Imports System.ComponentModel
Imports System.Collections

Public Class frmBangKeChungTu
    Dim TCNX As String = ""

    Private Sub frmBangKeChungTu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbNhapXuat.SelectedIndex = 0
        rbtKyQT.Checked = True
        KhoiTaocbbKyQuyetToan()
        cbbKYQT.SelectedIndex = 0
    End Sub

    Public Sub KhoiTaocbbKyQuyetToan()
        Dim ds As DataSet = BaseDB.ExecSql_DataSet("select * from tblKyQT")
        Dim row As DataRow
        row = ds.Tables(0).NewRow()
        row("id") = 0
        row("tenkyqt") = "Cả Năm"
        ds.Tables(0).Rows.Add(row)
        cbbKyQT.DisplayMember = "tenkyqt"
        cbbKyQT.ValueMember = "id"
        cbbKyQT.DataSource = ds.Tables(0)
    End Sub

#Region "txtTCNX"

    Private Sub txtTinhChatNhapXuat_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTinhChatNhapXuat.DoubleClick
        If (cbbNhapXuat.SelectedIndex = 0) Then
            ShowFormDoiTuong(enumDOITUONG.TINHCHATNHAP)
        Else 'xuat
            ShowFormDoiTuong(enumDOITUONG.TINHCHATXUAT)
        End If
    End Sub
    Private Sub txtTinhChatNhapXuat_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTinhChatNhapXuat.KeyDown
        If (e.KeyCode = Keys.F1) Then
            txtTinhChatNhapXuat_DoubleClick(txtTinhChatNhapXuat, Nothing)
        End If
    End Sub

    Public Sub ShowFormDoiTuong(ByVal loai As enumDOITUONG)
        Dim frm As New frmDMDoiTuong
        frm.bCallForm = True
        frm.eDoiTuong = loai
        frm.ShowDialog()
        If frm.selectedDT IsNot Nothing Then
            TCNX = frm.selectedDT.KH
            txtTinhChatNhapXuat.Text = frm.selectedDT.Ten
        Else
            TCNX = ""
            txtTinhChatNhapXuat.Text = ""
        End If
    End Sub
#End Region

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        Dim strsql As String
        strsql = "select * from viewDPNX "
        'co the ko can pha nay
        If (cbbNhapXuat.SelectedIndex = 0) Then
            strsql &= " where NX = 'N'"
        Else
            strsql &= " where NX = 'X'"
        End If
        If (TCNX <> "") Then
            strsql &= "and TCNX = '" & TCNX & "'"
        Else
            MsgBox("Bạn chưa chọn tính chất nhập xuất")
            Exit Sub
        End If
        'Dim tungay, denngay As String
        Dim thoigian, tieude As String
        Dim tongtien As Double
        

        If rbtKyQT.Checked Then
            If (cbbKYQT.SelectedValue <> 0) Then 'KyQT = Ca Nam
                strsql &= " AND KYQT = '" & cbbKYQT.SelectedValue & "'"
            End If
        Else
            strsql &= " AND NGAY >= '" & dtpTuNgay.Value.ToString("MM/dd/yyyy") & "' AND NGAY <= '" & dtpDenNgay.Value.ToString("MM/dd/yyyy") & "'"
        End If

        thoigian = cbbKYQT.SelectedText
        tieude = "Khoản: " + cbbNhapXuat.Text + "       Loại : " + TCNX + "       " + txtTinhChatNhapXuat.Text
        Dim ds As DataSet
        ds = BaseDB.ExecSql_DataSet(strsql)
        For Each row As DataRow In ds.Tables(0).Rows
            tongtien += row("TGIATRI")
        Next
        tongtien = Math.Round(tongtien)
        Dim thoi_gian As String
        If (cbbKYQT.SelectedValue <> 0) Then
            thoi_gian = cbbKYQT.Text + " - Năm " + CStr(Now.Year)
        Else
            thoi_gian = "Năm " + CStr(Now.Year)
        End If

        Dim nnreport As New rptBangKeChungTu
        nnreport.SetDataSource(ds.Tables(0))
        nnreport.SetParameterValue("tien_chu", DoiSoRaChu(tongtien))

        If rbtKyQT.Checked Then
            nnreport.SetParameterValue("thoigian", thoi_gian)
        Else
            nnreport.SetParameterValue("thoigian", "Từ ngày " + dtpTuNgay.Value.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenNgay.Value.ToString("dd/MM/yyyy"))
        End If

        'nnreport.SetParameterValue("thoigian", thoi_gian)
        nnreport.SetParameterValue("tieude", tieude)
        Dim frm As New frmReportViewer(nnreport)
        frm.ShowDialog()
    End Sub

    Private Sub rbtKyQT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtKyQT.CheckedChanged
        If rbtKyQT.Checked Then
            lblTuNgay.Visible = False
            lblDenNgay.Visible = False
            dtpTuNgay.Visible = False
            dtpDenNgay.Visible = False
            lblKyQT.Visible = True
            cbbKYQT.Visible = True
        Else
            lblTuNgay.Visible = True
            lblDenNgay.Visible = True
            dtpTuNgay.Visible = True
            dtpDenNgay.Visible = True
            lblKyQT.Visible = False
            cbbKYQT.Visible = False
        End If
    End Sub
End Class